package com.rzj.book.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：zbl_jc
 * 包路径：com.zbl.data
 * 类名称：WaveCurveFile
 * 类描述：  用于处理波形图的读取和存储的类
 * 创建人：张岚
 * 创建时间： 2020-05-11 15:08
 *
 *
 */
public class WaveCurveFile {

    private File file;

    private int pntCnt;

    private RandomAccessFile rf;

    public WaveCurveFile(File file, int pntCnt) {
        this.file = file;
        this.pntCnt = pntCnt;
    }

    public void openByRead() throws FileNotFoundException {
        this.rf = new RandomAccessFile(file,"r");
    }

    public void openByWrite() throws FileNotFoundException {
        this.rf = new RandomAccessFile(file,"rw");
    }

    public void write(List<Short> waveCurve) {
        for(Short data : waveCurve)
            try {
                this.rf.writeShort(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    public List<Short> read(int index) {
        List<Short> waveCurve = new ArrayList<Short>();
        for(int i=index * this.pntCnt;i<(index + 1) * this.pntCnt;i++) {
            try {
                waveCurve.add(this.rf.readShort());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return waveCurve;
    }

    public void close() throws IOException {
        this.rf.close();
    }

    public static void main(String[] args) {
//        File file = new File("D:\\runenv\\detectionfiles\\检测文件\\10000\\254\\627\\波形图14798_Y5-1.ZSW\\14798_Y5-1.ZSW.data");
        File file = new File("D:\\gitCode\\book\\book-web\\src\\test\\java\\com\\rzj\\bok\\userTest\\14798_Y5-1.ZSW");
        String name = file.getName().trim();
        String parent = file.getParent()+"\\波形图"+name+"\\"+file.getName()+".data";
        File file1 = new File(parent);
        WaveCurveFile waveCurveFile = new WaveCurveFile(file1,512);
        try {
            waveCurveFile.openByRead();
            List<Short> waveDatas = waveCurveFile.read(1);
            System.out.print(waveCurveFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                waveCurveFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
